package com.yuerlive.rainbow.module.live.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseFragment;
import com.yuerlive.rainbow.base.BasePresenter;
import com.yuerlive.rainbow.entity.Gift;
import com.yuerlive.rainbow.entity.LiveDetailData;
import com.yuerlive.rainbow.module.game.FragmentGameBase;
import com.yuerlive.rainbow.module.game.FragmentGameCatMouse;
import com.yuerlive.rainbow.module.game.FragmentGameNiuniu;
import com.yuerlive.rainbow.module.game.FragmentGameSanguo;
import com.yuerlive.rainbow.module.game.GameType;
import com.yuerlive.rainbow.module.playlive.PlayLiveRoomActivity;
import com.yuerlive.rainbow.observer.Observerable;
import com.yuerlive.rainbow.observer.ObserverableType;
import com.yuerlive.rainbow.utils.L;

/**
 * Created by xiaoyi on 2017/5/23.
 */

public class FragmentLayer extends BaseFragment {
    FragmentChatRoom fragmentChatRoom;
    FragmentGameBase gameFragment;
    private LiveDetailData liveDetailData;
    private boolean isGameShow;
    private Handler mHandler = new Handler();
    private int gameID;

    public static FragmentLayer newInstance(Bundle bundle) {
        FragmentLayer fragmentLayer = new FragmentLayer();
        fragmentLayer.setArguments(bundle);
        return fragmentLayer;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_content;
    }

    @Override
    public void initViews(View root) {
        fragmentChatRoom = new FragmentChatRoom();
        fragmentChatRoom.setArguments(getArguments());
        if (getArguments().containsKey("gameID"))
            gameID = getArguments().getInt("gameID");
        getChildFragmentManager().beginTransaction().add(R.id.content, fragmentChatRoom, "FragmentChatRoom")
                .show(fragmentChatRoom)
                .commit();

    }

    private void initGame(GameType gameType) {
        if (gameFragment != null) {
            getChildFragmentManager().beginTransaction()
                    .setCustomAnimations(R.anim.game_in, R.anim.game_out)
                    .remove(gameFragment).commit();
        }
        switch (gameType) {
            case niuniu:
                gameFragment = new FragmentGameNiuniu();
                break;
            case catmouse:
                gameFragment = new FragmentGameCatMouse();
                break;
            case sanguo:
                gameFragment = new FragmentGameSanguo();
        }
        if (gameFragment == null) return;
        gameFragment.setLiveDetail(liveDetailData);
        try {
            if (isAdded()) {
                fragmentChatRoom.setChatViewLoacation(0);
                getChildFragmentManager().beginTransaction().add(R.id.content, gameFragment, "FragmentGame")
                        .setCustomAnimations(R.anim.game_in, R.anim.game_out)
                        .show(gameFragment)
                        .commit();
            } else mHandler.postDelayed(() -> {
                fragmentChatRoom.setChatViewLoacation(0);
                addFragmentDelay();
            }, 500);
        } catch (Exception e) {
            e.printStackTrace();
        }

        isGameShow = true;
    }

    private void addFragmentDelay() {
        if (isAdded()) {
            getChildFragmentManager().beginTransaction().add(R.id.content, gameFragment, "FragmentGame")
                    .show(gameFragment)
                    .commit();
        } else {
            mHandler.postDelayed(() -> {
                addFragmentDelay();
            }, 500);
        }
    }

    public void deleteGame() {
        L.e(TAG,"AAAAAAAAAAAAAAAAAAAA  deleteGame");
        Observerable.getInstance().notifyChange(ObserverableType.ISBANKER, false);
        if (gameFragment != null) {
            getChildFragmentManager().beginTransaction()
                    .setCustomAnimations(R.anim.game_in, R.anim.game_out)
                    .remove(gameFragment).commit();
            fragmentChatRoom.setChatViewLoacation(200);
            gameFragment = null;
        }
        if (mActivity instanceof PlayLiveRoomActivity) {
            ((PlayLiveRoomActivity) mActivity).setGameID(0);
        }
    }

    public void setLiveDetail(LiveDetailData liveDetail) {
        this.liveDetailData = liveDetail;
        if (mActivity instanceof PlayLiveRoomActivity) {
            gameID = ((PlayLiveRoomActivity) mActivity).getGameId();
        } else {
            if (liveDetailData != null && liveDetail.getGameInfo() != null)
                gameID = liveDetail.getGameInfo().getId();
            else
                gameID = 0;
        }
        initGame(GameType.valueOf(gameID));
        if (fragmentChatRoom != null) {
            fragmentChatRoom.setLiveDetail(liveDetail);
        } else {
            mHandler.postDelayed(() -> {
                if (fragmentChatRoom != null) {
                    fragmentChatRoom.setLiveDetail(liveDetail);
                }
            }, 500);
        }
    }

    public void hideGameWithAnim() {
        L.e(TAG,"AAAAAAAAAAAAAAAAAAAA  hideGameWithAnim");
        if (gameFragment == null) return;
        getChildFragmentManager().beginTransaction()
                .setCustomAnimations(R.anim.fragment_in, R.anim.fragment_out)
                .hide(gameFragment).commit();
        isGameShow = false;
    }

    public void showGameWithAnim() {
        if (gameFragment != null) {
            getChildFragmentManager().beginTransaction()
                    .setCustomAnimations(R.anim.fragment_in, R.anim.fragment_out)
                    .show(gameFragment).commit();
            isGameShow = true;
        }
    }

    public void hideGame() {
        L.e(TAG,"AAAAAAAAAAAAAAAAAAAAAAAAA   hideGame");
        if (gameFragment != null) {
            getChildFragmentManager().beginTransaction().hide(gameFragment).commit();
            isGameShow = false;
        }
    }

    public void showGame(int gameID) {
        initGame(GameType.valueOf(gameID));
    }

    public boolean isShowGame() {
        return isGameShow;
    }

    public boolean isDialogShowing() {
        boolean isShowing = fragmentChatRoom != null && fragmentChatRoom.isDialogShowing();
        if (isShowing) return isShowing;
        isShowing = gameFragment != null && gameFragment.isDialogShowing();
        return isShowing;
    }

    public void sendNotifyMessage(String msg) {
        fragmentChatRoom.sendNotifyMessage(msg);
    }

    public void sendNotifyMessage(String msg, boolean send) {
        fragmentChatRoom.sendNotifyMessage(msg, send);
    }

    public void switchGame(int gameID) {
        L.e(TAG,"BBBBBBBBBB  444444444   deleteG");
        deleteGame();
        int delayTime = 500;
        if (mActivity instanceof PlayLiveRoomActivity) {
            delayTime = 500;
        } else {
            delayTime = 1000;
        }
        mHandler.postDelayed(() -> {
            initGame(GameType.valueOf(gameID));
        }, delayTime);
    }

    public int getGameID() {
        if (gameFragment == null) return 0;
        if (gameFragment instanceof FragmentGameNiuniu) {
            return 1;
        } else if (gameFragment instanceof FragmentGameCatMouse) {
            return 2;
        } else if (gameFragment instanceof FragmentGameSanguo) {
            return 3;
        }
        return 0;
    }

    public void sendGift(Gift gift) {
        fragmentChatRoom.sendGift(gift, 0);
    }
}
